package com.example.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mall.common.Result;
import com.example.mall.domain.entity.Product;
import com.example.mall.service.ProductService;
import com.example.mall.domain.vo.ProductVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商品管理")
@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Resource
    private ProductService productService;

    @Operation(summary = "获取商品列表")
    @GetMapping
    public Result<Page<ProductVO>> list(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        @RequestParam(required = false) String name,
                                        @RequestParam(required = false) String categoryId) {
        return Result.success(productService.pageListProduct(name,categoryId, page, size));
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public Result<String> save(@RequestBody Product product) {
        productService.saveOrUpdate(product);
        return Result.success();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{id}")
    public Result<String> remove(@PathVariable Long id) {
        productService.removeById(id);
        return Result.success();
    }
} 